﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace D20.Entities.Characters
{
    /// <summary>
    /// A relationship.
    /// </summary>
    public struct Relationship
    {
        /// <summary>
        /// Indicates whether this instance and a specified object are equal.
        /// </summary>
        /// <param name="obj">Another object to compare to.</param>
        /// <returns>
        /// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj is Relationship)
            {
                return this == (Relationship)obj;
            }
            return false;
        }
        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Relationship left, Relationship right)
        {
            return (left.Intensity == right.Intensity) && ReferenceEquals(left.Target, right.Target);
        }
        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Relationship left, Relationship right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Returns the hash code for this instance.
        /// </summary>
        /// <returns>
        /// A 32-bit signed integer that is the hash code for this instance.
        /// </returns>
        public override int GetHashCode()
        {
            if (target == null)
            {
                return 0;
            }
            return intensity.GetHashCode() ^ target.GetHashCode();
        }
        private Creature target;
        private float intensity;
        /// <summary>
        /// The target creature.
        /// </summary>
        public Creature Target
        {
            get { return target; }
            set
            {
                Debug.Assert(value != null, "value != null");
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                target = value;
            }
        }
        /// <summary>
        /// The intensity of the relationship. Positive values are like, negative values are dislike.
        /// </summary>
        public float Intensity { get { return intensity; } set { intensity = value; } }

        /// <summary>
        /// Initializes a new instance of the <see cref="Relationship"/> struct.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="intensity">The intensity.</param>
        public Relationship(Creature target, float intensity)
        {
            Debug.Assert(target != null, "target != null");
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            this.target = target;
            this.intensity = intensity;
        }
    }
}
